package com.tanhua.dubbo.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.api.UsersApi;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
public class UsersApiImpl implements UsersApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String saveUsers(Long userId, Long friendId) {
        if (!ObjectUtil.isAllNotEmpty(userId, friendId)) {
            return null;
        }
        //检测该好友关系是否存在
        Query query = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        long count = mongoTemplate.count(query, Users.class);
        if (count > 0) {
            //存在就返回
            return null;
        }

        //建立我与好友的好友关系
        Users users = new Users();
        users.setId(ObjectId.get());
        users.setUserId(userId);
        users.setFriendId(friendId);
        users.setDate(System.currentTimeMillis());
        mongoTemplate.save(users);

        //建立好友与我的好友关系
        users.setId(ObjectId.get());
        users.setUserId(friendId);
        users.setFriendId(userId);
        mongoTemplate.save(users);

        //成功返回
        return users.getId().toHexString();
    }

    @Override
    public Boolean removeUsers(Long userId, Long friendId) {
        //删除我与好友的关系
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        long count1 = mongoTemplate.remove(query1, Users.class).getDeletedCount();
        //删除好友与我的关系
        Query query2 = Query.query(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        long count2 = mongoTemplate.remove(query2, Users.class).getDeletedCount();
        return count1 > 0 && count2 > 0;
    }

    @Override
    public List<Users> queryAllUsersList(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query, Users.class);
    }

    @Override
    public PageInfo<Users> queryUsersList(Long userId, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageRequest);
        List<Users> usersList = mongoTemplate.find(query, Users.class);
        PageInfo<Users> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(usersList);
        return pageInfo;
    }
}
